import http from '../../../common/httpUtil'
import * as types from './recommendMutationTypes'

const actions = {
  queryRecommend ({commit}, {fundtype} = {}) {
    commit(types.SYS_FILE_UPLOAD_QUERY_RECOMMENDS_BEGIN)
    var isOrg
    if (!fundtype || fundtype === 'user') {
      isOrg = 0
    } else if (fundtype === 'org') {
      isOrg = 1
    }
    return http.get(`admin/config/t0_list?isOrg=${isOrg}`).then(data => {
      console.log(data)
      var Recommends = data.t0Models
      console.log(Recommends)
      for (var k = 0; k < Recommends.length; k++) {
        if (Recommends[k].limits && Recommends[k].limits.length > 0) {
          if (Recommends[k].limits[0].minValue) {
            Recommends[k].minMoney = Recommends[k].limits[0].minValue
          } else {
            Recommends[k].minMoney = ''
          }
          if (Recommends[k].limits[0].reminder) {
            Recommends[k].reminder = Recommends[k].limits[0].reminder
          } else {
            Recommends[k].reminder = ''
          }

        } else {
          Recommends[k].minMoney = ''
          Recommends[k].reminder = ''
        }
      }
      if (data.activeFundCode === '-1') {
        for (var i = 0; i < Recommends.length; i++) {
          Recommends[i].state = '0'
        }
      } else {
        for (var j = 0; j < Recommends.length; j++) {
          if (Recommends[j].fundCode === data.activeFundCode) {
            Recommends[j].state = '1'
          } else {
            Recommends[j].state = '0'
          }
        }
      }
      console.log(Recommends)
      commit(types.SYS_FILE_UPLOAD_QUERY_RECOMMENDS_SUC, {Recommends})
    })
  },
  editRecommend ({commit}, {fundCode}) {
    // return http.post(`banklimit/update?id=${formData.id}&singleLimit=${formData.singleLimit}&dayLimit=${formData.dayLimit}&monthLimit=${formData.monthLimit}&state=${formData.state}`).then(data => {
    //   console.log(data)
    //   commit(types.SYS_FILE_UPLOAD_UPDATE_SUC, {formData})
    //   return data
    // });
    return http.post(`admin/config/set_t0?fundCode=${fundCode}`).then(data => {
      console.log(data)
      commit(types.SYS_RECOMMENDS_UPLOAD_UPDATE_SUC, {fundCode})
    })
  },
  eidtFund ({commit}, {formData}) {
    return http.post(`admin/config/add_or_update_t0_fund_trade_limit?fundCode=${formData.fundCode}&minMoney=${formData.minMoney}&reminder=${formData.reminder}&userOrOrg=${formData.userOrOrg}`).then(data => {
      console.log(data)
      return data
    })
  }
}

export default actions
